﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Lead2012.Business
{
    public class FieldRepository
    {
        // Create an instance of entity
        Lead2012Entities entities = new Lead2012Entities();

        public List<Field> GetAll()
        {
            return (from c in entities.Fields select c).ToList();
        }
        
        public Field GetSingle(int id)
        {
            return (from _object in entities.Fields where _object.FieldId == id select _object).FirstOrDefault();
        }

        public void Create(Field _object)
        {
            entities.AddToFields(_object);
            entities.SaveChanges();
        }
        public void Create(Field _object, int typeId)
        {
            _object.FieldTypeReference.EntityKey = new EntityKey("Lead2012Entities.FieldTypes", "TypeId", typeId);
            entities.AddToFields(_object);
            entities.SaveChanges();
        }
        public void Update(Field _object, int typeId)
        {
            _object.FieldTypeReference.EntityKey = new EntityKey("Lead2012Entities.FieldTypes", "TypeId", typeId);
            entities.SaveChanges();
        }
        public void Update(Field _object)
        {
            entities.SaveChanges();
        }
        public void Delete(int id)
        {
            var _object = (from _list in entities.Fields where _list.FieldId == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }
        public void Save(Field _object)
        {
            _object.EntityKey = (from Field ac in entities.Fields where ac.FieldId == _object.FieldId select ac).FirstOrDefault().EntityKey;
            entities.SaveChanges();
        }
    }
}